package com.patrol.patrol.ui.appear;

import android.content.Context;
import android.os.Build;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.android.arouter.launcher.ARouter;
import com.google.gson.Gson;
import com.patrol.patrol.MyApplication;
import com.patrol.patrol.R;
import com.patrol.patrol.data.EventModel;

import java.util.ArrayList;
import java.util.List;

public class ListAdapter extends RecyclerView.Adapter {
    Context context;
    List<EventModel> modelList = new ArrayList<>();
    public ListAdapter(Context context,List<EventModel> modelList) {
        this.context = context;
        this.modelList = modelList;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_list, parent, false);
        return new ViewHolder(view);
    }

    @RequiresApi(api = Build.VERSION_CODES.N)
    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int position) {
        ViewHolder holder = (ViewHolder)viewHolder;
        EventModel model = modelList.get(position);
        holder.equipTitle.setText(model.getEtName());

        AppearActivity.spSexDataList.forEach(item->{
            if(model.getSpSex().equals(item.getKey())){
                holder.equipSubtitle.setText(item.getVal());
            }
        });

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ARouter.getInstance().build("/app/AppearActivity")
                        .withString("eventModelJson",new Gson().toJson(model))
                        .navigation();
            }
        });
    }

    @Override
    public int getItemCount() {
        return modelList.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder{

        TextView equipTitle;
        TextView equipSubtitle;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            equipTitle = itemView.findViewById(R.id.equip_title);
            equipSubtitle = itemView.findViewById(R.id.equip_subtitle);
        }
    }
}
